define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    cardInfo: {},
                    store:{},
                    loading:0,
                    cardQrcodeInfo:{},
                    downUrl:''
                    
                };
            },
            el: '#pageOpencard',
            created: function() {
                this.fetchData();
            },
            methods: {
                fetchData: function() {
                    var _this = this;
                    if(app.isWeiXin()){
                      this.openCard();
                    }else if(app.isAlipay()){
                      // var that = this;
                      app.confirm({
                         'text':'请在微信中领取会员卡，如已领取请在支付宝中绑定手机号即可享会员权益',
                         'yes':function(){
                            window.location.href = '/pages/user/?store_id='+app.getStoreId()+'#/userInfo';
                         }
                      });
                    }else{
                      this.$router.push({ name: 'cardDetail', params: { id: this.$route.params.id } });
                    }
                    
                },
                // 保存卡券到数库 
                saveCard:function(code){
                  var user_info = app.getUserInfo();
                  app.post("mall/card/saveForWx",{
                        user_id : user_info.user_id,
                        p_user_id : this.$route.params.p_user_id,
                        cardId:this.$route.params.id,
                        code:code,
                        store_id:user_info.store_id,
                        shop_id:0,

                    },function(res){
                      if (res.code != 200) {
                         app.alert(res.msg);
                         return ;
                      }
                      app.alert("领取成功",function(){
                         app.wx.closeWindow();
                      });
                       
                    });
                },
                  //微信打开卡包
                openCard:function(){
                   var user_info = app.getUserInfo();
                  var outer_str = this.$route.params.p_user_id+"_"+user_info.user_id;
                   var that = this;
                   var card_id = this.$route.params.id;
                    app.showLoad();
                    app.get("apiv2/card/openCard", {
                      card_id : card_id,
                      p_user_id : this.$route.params.p_user_id,
                    },function(res){
                      if (res.code != 200) {
                         if(res.code==3){
                          window.location.href=app.params.apiDomain+"/apiv2/scene/bindWxCard?id="+card_id;
                          return ;
                         }
                         app.alert(res.msg);
                         return ;
                      }
                      var data = res.data;
                       app.wxJs(function(wx) { 
                        app.showLoad();
                       
                        wx.addCard({
                            cardList: [
                              {
                                cardId: data.card_id,
                                cardExt: '{"code": "'+data.code+'","outer_str":"'+outer_str+'", "nonce_str":"'+data.nonce_str+'", "timestamp": "'+data.timestamp+'", "signature":"'+data.signature+'"}'
                              }
                            ],
                            success: function (res) {
                              that.saveCard(data.code);
                            },
                            cancel: function (res) {
                              wx.closeWindow();
                            }
                          });
                         
                          
                      });
                      
                    });

                }
                 
            }

        }).$mount('#app');
    };
    module.exports = controller;
});
